<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <crudOperation :permission="permission">
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="等级编码" prop="code">
            <el-input v-model="form.memberCode" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="等级名称" prop="name">
            <el-input v-model="form.memberName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="折扣率" prop="name">
            <el-input-number :step="0.1" :min="0" :max="100"  v-model="form.discountRate" style="width: 100%;"/>
          </el-form-item>
          <el-form-item label="是否默认" prop="isAppend">
            <el-select
              v-model="form.isDefault"
              class="filter-item"
              clearable
              placeholder="是否默认"
              size="small"
              style="width: 100%"
            >
              <el-option
                v-for="item in  dict.common_yes_or_no"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>


      <!--表格渲染-->
      <el-table ref="table"
                border
                height="700"
                show-summary
                @cell-dblclick="row=>crud.toEdit(row)"
                v-loading="crud.loading"
                :data="crud.data"
                size="small"
                style="width: 100%;"
                @header-dragend="changeColWidth"
                :summary-method="crud.sumRow"
                @row-click="crud.selectRow"
                :row-style="crud.rowClass"
                :row-class-name="crud.tableRowClassName"
                @selection-change="crud.selectionChangeHandler"
                border row-key="id" >
        <el-table-column :render-header="crud.renderHeader" type="selection" width="60"/>
        <el-table-column :render-header="crud.renderHeader" type="index" width="60" label="序号"/>
        <el-table-column :render-header="crud.renderHeader"  label="会员等级编码" prop="memberCode" />
        <el-table-column :render-header="crud.renderHeader"  label="会员等级名称" prop="memberName" />
        <el-table-column :render-header="crud.renderHeader"  label="折扣率" prop="discountRate" />
        <el-table-column :render-header="crud.renderHeader" v-permission="['admin','basGoodsType:edit','basGoodsType:del']" label="操作" width="150px"
                         align="center">
          <template slot-scope="scope">
            <udOperation
                :data="scope.row"
                :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination layout='prev, pager, next, sizes'/>
    </div>
  </div>
</template>

<script>
import crudBasGoodsType from '@/api/erp/memberLevel'
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Treeselect from "@riophae/vue-treeselect";

import { publicListKey } from "@/utils/key";
import {getHeaderProperties} from "@/api/erp/headerProperties";
import {tablemixins} from "@/mixins/tablemixins";
import  jTableColumnConfig  from "@/components/jTableColumnConfig/jTableColumnConfig";
import { getToken } from "@/utils/auth";

// crud交由presenter持有
const defaultCrud = CRUD({title: '商品分类', url: 'api/basMemberLevel', sort: 'id,desc', crudMethod: {...crudBasGoodsType}})
const defaultForm = {
  id: null,
  memberCode:null,
  memberName:null,
  iscountRate:null,
  isDefault:null,
  createUserId: null,
  createUserCode: null,
  createUserName: null,
  createTime: null,
  updateUserId: null,
  updateUserCode: null,
  updateUserName: null,
  updateTime: null
}
export default {
  name: 'memberLevel',
  components: {pagination, crudOperation, rrOperation, udOperation,Treeselect,jTableColumnConfig},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud(),publicListKey,tablemixins],
  dicts: [ 'common_yes_or_no'],
  data() {
    return {

      permission: {
        add: ['admin', 'basMemberLevel:add'],
        edit: ['admin', 'basMemberLevel:edit'],
        del: ['admin', 'basMemberLevel:del'],
        import: ['admin', 'basMemberLevel:import'],
        export: ['admin', 'basMemberLevel:export'],
        copy: ['admin', 'basMemberLevel:copy'],
        upload: ['admin', 'basMemberLevel:upload'],
        download: ['admin', 'basMemberLevel:download'],
        enable: ['admin', 'basMemberLevel:enable'],
        disable: ['admin', 'basMemberLevel:disable'],
      },
      rules: {
        memberName: [
          {required: true, message: '名称不能为空', trigger: 'blur'}
        ],
        memberCode: [
          {required: true, message: '编码不能为空', trigger: 'blur'},
        ],
      },
      treeList: [],
      defaultProps: {children: 'children', label: 'name', code: 'code'},
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: getToken() },
        // 上传的地址
        url: '',
        // 上传的文件列表
        fileList: [],

        imgList:[]
      },
    }
  },
  watch: {
    // "form.imageUrl"(){
    //   this.upload.imgList = JSON.parse(this.form.imageUrl)
    // }
  },
  updated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })},
  activated() {
    this.upload.url = window.location.href.split('/').slice(0,3).join('/')+'/api/common/uploadImage'
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      //获取树型数据
      getBasGoodsTypeTree().then(res => {
        // this.treeList  = res.data
        this.treeList = []
        const treeList = {id: 0, name: '顶级分类', children: []}
        treeList.children = res.data
        this.treeList.push(treeList)

      })

    },
    normalizer(node) {
      //去掉children=[]的children属性
      if (node.children == null || node.children.length == 0) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },

    submitUpload() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.form.imageUrl = response.data.path
      this.upload.isUploading = false;
      this.upload.fileList = []
      this.form.imageUrl = response.data.path
    },
    delImage(index) {
      this.$confirm('此操作将永久删除该图片, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        this.form.imageUrl = ''
      }).catch(() => {
      })
    },
  }
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
